package com.xiaoyu.client.ui.activity.main.mine.my_info;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.techfansy.provinceCitySelect.ProvinceCitySelect;
import com.xiaoyu.client.R;
import com.xiaoyu.client.model.mine.MineBean;
import com.xiaoyu.client.network.NetworkManager;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.base.BaseActivityWithTitle;
import com.xiaoyu.commonlib.utils.CheckFormatUtils;
import com.xiaoyu.commonlib.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author jx
 * @date 创建时间 2018/10/15
 * @Description 管理收货地址
 */
public class ShippingAddressActivity extends BaseActivityWithTitle {

    @BindView(R.id.activity_shipping_address_area_txt)
    TextView mAddressAreaTxt;
    @BindView(R.id.activity_ship_address_details)
    EditText mEditDetails;
    @BindView(R.id.activity_ship_address_receiver)
    EditText mReceiver;
    @BindView(R.id.activity_ship_address_tel)
    EditText mTel;
    private TextView mRightTxt;
    private String provinces, city, county;//省市区
    private String addressId;


    @Override
    protected void loadContentView(ViewGroup parent) {
        setTitleText("管理收货地址");
        View mView = LayoutInflater.from(this).inflate(R.layout.activity_shipping_address, parent, true);
        ButterKnife.bind(this);
        initView();
    }

    //初始化数据
    private void initView() {
        addRightTxt();

        //上一页面点击编辑跳转到该页面
        if (getIntent().getStringExtra(AddressActivity.ADDRESS_ID) != null) {
            addressId = getIntent().getStringExtra(AddressActivity.ADDRESS_ID);
            getAddressInfo();
        }
    }

    //添加右侧textview
    private void addRightTxt() {
        mRightTxt = new TextView(this);
        mRightTxt.setText("确定");
        mRightTxt.setTextColor(getResources().getColor(R.color.color_blue_27));
        mRightTxt.setTextSize(15);
        mRightTxt.setOnClickListener(this);
        addRightView(mRightTxt, 0);
    }


    //所选区域
    @OnClick(R.id.activity_shipping_address_area)
    public void addressAreaClick() {
        new ProvinceCitySelect().createBottom(ShippingAddressActivity.this).
                setSureListener(new ProvinceCitySelect.OnClickListener() {
            @Override
            public void sure(String strProvince, String strCity, String strDistrict) {
                provinces = strProvince;
                city = strCity;
                county = strDistrict;
                mAddressAreaTxt.setText(strProvince + strCity + strDistrict);
            }
        }).show();

    }

    //填写完信息上传
    private void addAddress() {
        NetworkManager.addAddress(provinces, city, county, mEditDetails.getText().toString(), mReceiver.getText().toString(), mTel.getText().toString(), new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                finish();
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == mRightTxt) {//保存按钮的点击事件
            if (TextUtils.isEmpty(mReceiver.getText().toString()) || TextUtils.isEmpty(mTel.getText().toString())
                    || TextUtils.isEmpty(mAddressAreaTxt.getText().toString()) || TextUtils.isEmpty(mEditDetails.getText().toString())) {
                ToastUtil.showToast(ShippingAddressActivity.this, "请输入完整信息");
                return;
            }
            if (CheckFormatUtils.checkMobilePhone(mTel.getText().toString()) == 2) {
                ToastUtil.showToast(this, "手机号不合法");
                return;
            }

            //上一页面点击编辑跳转到该页面
            if (getIntent().getStringExtra(AddressActivity.ADDRESS_ID) != null) {
                confirmFinishAddress();
            } else {
                addAddress();
            }
        }
    }

    private void getAddressInfo() {
        NetworkManager.confirmAddress(addressId, new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                succeedResult(result);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
            }
        });
    }

    //解析显示数据
    private void succeedResult(String result) {
        MineBean bean = new Gson().fromJson(result, MineBean.class);
        mReceiver.setText(bean.getData().getReceiver());
        mTel.setText(bean.getData().getTel());
        mAddressAreaTxt.setText(bean.getData().getProvinces() + bean.getData().getCity() + bean.getData().getCounty());
        mEditDetails.setText(bean.getData().getDetail());
    }


    //修改地址之后提交
    private void confirmFinishAddress() {
        NetworkManager.confirmFinishAddress(provinces, city, county, mEditDetails.getText().toString(), mReceiver.getText().toString(), mTel.getText().toString(), addressId, new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                finish();
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
            }
        });
    }

}
